#ifndef APPOINTMENTWIDGET_H
#define APPOINTMENTWIDGET_H

#include <QWidget>
#include <QJsonObject>
#include <QJsonArray>
#include <QMap>
#include <QDateTime>
#include "../../../Common/models.h"
#include "../../../Common/protocol.h"

class QLabel;  // 前向声明

namespace Ui {
class AppointmentWidget;
}

class AppointmentWidget : public QWidget {
    Q_OBJECT
    
public:
    explicit AppointmentWidget(const Doctor& doctor, QWidget *parent = nullptr);
    ~AppointmentWidget();
    
signals:
    void showPatientDetail(int patientId, int appointmentId);  // 显示患者详情的信号
    
private slots:
    void on_dateEdit_dateChanged(const QDate& date);
    void on_refreshButton_clicked();
    void on_appointmentTable_cellDoubleClicked(int row, int column);
    void onMessageReceived(MessageType type, const QJsonObject& data);
    void onStartConsultation(int appointmentId, int patientId);     // 开始接诊
    void onFinishConsultation(int appointmentId);                   // 完成就诊
    
private:
    void loadAppointments();
    void updateAppointmentTable(const QJsonArray& appointments);
    bool getCachedData(const QDate& date, QJsonArray& data);  // 从缓存获取数据
    void cacheData(const QDate& date, const QJsonArray& data); // 缓存数据
    
    Ui::AppointmentWidget *ui;
    Doctor m_doctor;
    QJsonArray m_appointments;  // 存储预约数据
    
    // 缓存机制
    struct CacheEntry {
        QJsonArray data;
        QDateTime timestamp;
    };
    QMap<QString, CacheEntry> m_cache;  // 日期 -> 缓存数据
    static const int CACHE_DURATION_SECONDS = 60;  // 缓存有效期：60秒
    
    // 加载状态
    QLabel* m_loadingLabel;  // 加载提示标签
    bool m_isLoading;        // 是否正在加载
    
    void showLoading();      // 显示加载状态
    void hideLoading();      // 隐藏加载状态
};

#endif // APPOINTMENTWIDGET_H

